#include <stdio.h>
#include <string.h>
#include "utils.h"
#include "heap_sort.h"
#include "quick_sort.h"
#include "insert_sort.h"
#include "merge_sort.h"
#include "pop_sort.h"


void sort_compare(int len, double ave_time[5]) {
    int data[len];
    int data_copy[len];
    int data_copy2[len];
    int data_copy3[len];
    int data_copy4[len];
    for (int i = 0; i < len; ++i) {
        data[i] = random_int_1000000();
    }
    // 数据拷贝
    memcpy(data_copy, data, len * sizeof(int));
    memcpy(data_copy2, data, len * sizeof(int));
    memcpy(data_copy3, data, len * sizeof(int));
    memcpy(data_copy4, data, len * sizeof(int));


    clock_t start1, start2, start3, start4, start5, finish1, finish2, finish3, finish4, finish5;
    double duration1, duration2, duration3, duration4, duration5;

    start1 = clock();
    heap_sort(data, len);
    finish1 = clock();

    start2 = clock();
    quick_sort(data_copy, 0, len - 1);
    finish2 = clock();

    start3 = clock();
    insert_sort(data_copy2, len);
    finish3 = clock();

    start4 = clock();
    merge_sort(data_copy3, 0, len);
    finish4 = clock();

    start5 = clock();
    pop_sort(data_copy4, len);
    finish5 = clock();


    duration1 = (double) (finish1 - start1) / CLOCKS_PER_SEC;
    ave_time[0] = duration1;
//    printf("heap_sort costs: %f seconds\n", duration1);


    duration2 = (double) (finish2 - start2) / CLOCKS_PER_SEC;
    ave_time[1] = duration2;
//    printf("quick_sort costs: %f seconds\n", duration2);


    duration3 = (double) (finish3 - start3) / CLOCKS_PER_SEC;
    ave_time[2] = duration3;
//    printf("insert_sort costs: %f seconds\n", duration3);

    duration4 = (double) (finish4 - start4) / CLOCKS_PER_SEC;
    ave_time[3] = duration4;
//    printf("insert_sort costs: %f seconds\n", duration4);

    duration5 = (double) (finish5 - start5) / CLOCKS_PER_SEC;
    ave_time[4] = duration5;
//    printf("insert_sort costs: %f seconds\n", duration5);
}


int main(int argc, char **argv) {
    srand(time(NULL));
    int length = 100000;
    int epochs = 100;
    double ave_times[100][5] = {0.0};

    for (int i = 0; i < epochs; ++i) {
        sort_compare(length, ave_times[i]);
    }
    double heap_ave = 0.0, quick_ave = 0.0, insert_ave = 0.0, merge_ave = 0.0, pop_ave = 0.0;
    for (int j = 0; j < epochs; ++j) {
        heap_ave += ave_times[j][0];
        quick_ave += ave_times[j][1];
        insert_ave += ave_times[j][2];
        merge_ave += ave_times[j][3];
        pop_ave += ave_times[j][4];
    }

    printf("heap_sort average costs: %f seconds\n", heap_ave / length);
    printf("quick_sort average costs: %f seconds\n", quick_ave / length);
    printf("insert_sort average costs: %f seconds\n", insert_ave / length);
    printf("merge_sort average costs: %f seconds\n", merge_ave / length);
    printf("pop_sort average costs: %f seconds\n", pop_ave / length);
}